﻿Ext.define("PSI.OutPlan.ReportCntNumForm", {
    createUI: function (entity) {
        var me = this;
        me.entity = entity;

        var wnd = Ext.create("Ext.window.Window", {
            title: "上报箱号",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 450,
            height: 300,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 90,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "id",
                            value: entity.id
                        },
                        {
                            xtype: "displayfield",
                            value: entity.planNumber,
                            fieldLabel: "计划号"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.address,
                            fieldLabel: "门点"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.takeCntDT,
                            fieldLabel: "提箱时限"
                        },
                        {
                            id: "editTruckCompany",
                            fieldLabel: "拖车公司",
                            xtype: "displayfield",
                            value: entity.truckCompany
                        },
                        {
                            id: "editTruckNumber",
                            xtype: "displayfield",
                            fieldLabel: "车号",
                            value: entity.truckNumber
                        },
                        {
                            id: "editDriver",
                            xtype: "displayfield",
                            fieldLabel: "司机",
                            value: entity.driver
                        }, {
                            id: "editDriverTel",
                            xtype: "displayfield",
                            fieldLabel: "司机电话",
                            value: entity.dirverTel
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "箱号/封号/箱型",
                            layout: "hbox",
                            msgTarget: 'side',
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            items: [
                                {
                                    id: "editCntNumber1",
                                    name: "cntNumber1",
                                    xtype: "textfield",
                                    flex: 1,
                                    margin: "0 5 0 0",
                                    maxLength: 11,
                                    allowBlank: false,
                                    blankText: "没有输入箱号"
                                },
                                {
                                    id: "editCntSeal1",
                                    name: "cntSeal1",
                                    xtype: "textfield",
                                    margin: "0 5 0 0",
                                    flex: 1
                                },
                                {
                                    id: "editCntType1",
                                    name: "cntType1",
                                    xtype: "psi_bc_cnttypefield",
                                    flex: 1,
                                    allowBlank: false,
                                    blankText: "没有输入箱型"
                                }
                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "箱号/封号/箱型",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editCntNumber2",
                                    name: "cntNumber2",
                                    xtype: "textfield",
                                    maxLength: 11,
                                    margin: "0 5 0 0",
                                    flex: 1
                                },
                                {
                                    id: "editCntSeal2",
                                    name: "cntSeal2",
                                    xtype: "textfield",
                                    margin: "0 5 0 0",
                                    flex: 1
                                },
                                {
                                    id: "editCntType2",
                                    name: "cntType2",
                                    xtype: "psi_bc_cnttypefield",
                                    flex: 1
                                }
                            ]
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: me.onOK,
                            scope: me
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: me
                        }
                    ]
                }
            ]

        });

        me.wnd = wnd

        Ext.getCmp("editCntNumber1").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editCntSeal1").focus();
            }
        }, me);
        Ext.getCmp("editCntSeal1").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editCntType1").focus();
            }
        }, me);
        Ext.getCmp("editCntType1").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editCntNumber2").focus();
            }
        }, me);
        Ext.getCmp("editCntNumber2").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editCntSeal2").focus();
            }
        }, me);
        Ext.getCmp("editCntSeal2").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editCntType2").focus();
            }
        }, me);
    },

    show: function (parentForm, entity) {
        var me = this;

        me.parentForm = parentForm;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(entity); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (entity) {
        Ext.getCmp("editCntNumber1").focus();

        var me = this;
        var form = me.wnd;
        var el = me.wnd.getEl() || Ext.getBody();

        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/OutPlan/GetCntNumber",
            method: "POST",
            params: { id: entity.id },
            callback: function (options, success, response) {
                el.unmask();
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    me.setEntityData(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });
    },

    setEntityData: function (data) {
        Ext.getCmp("editCntNumber1").setValue(data.cntNumber1);
        Ext.getCmp("editCntSeal1").setValue(data.cntSeal1);
        Ext.getCmp("editCntType1").setValue(data.cntType1);

        Ext.getCmp("editCntNumber2").setValue(data.cntNumber2);
        Ext.getCmp("editCntSeal2").setValue(data.cntSeal2);
        Ext.getCmp("editCntType2").setValue(data.cntType2);
    },

    // private
    onOK: function () {
        var me = this;

        var f = Ext.getCmp("editForm");
        var el = f.getEl() || Ext.getBody();
        var cntNumber1 = Ext.getCmp("editCntNumber1").getValue();
        var cntNumber2 = Ext.getCmp("editCntNumber2").getValue();
        var funcSubmit = function () {
            f.submit({
                url: "/OutPlan/ReportCntNum",
                method: "POST",
                success: function (form, action) {
                    el.unmask();
                    me.wnd.close();
                    me.parentForm.refreshPlanGrid();
                },
                failure: function (form, action) {
                    el.unmask();
                    PSI.MsgBox.showInfo(action.result.msg, function () {
                        Ext.getCmp("editCntNum1").focus();
                    });
                }
            });
        };

        el.mask("数据保存中...");
        Ext.Ajax.request({
            url: "/OutPlan/CheckCntNumber",
            method: "POST",
            params: { cntNumber1: cntNumber1, cntNumber2: cntNumber2 },
            callback: function (options, success, response) {
                el.unmask();
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    if (data.success) {
                        funcSubmit();
                    } else {
                        PSI.MsgBox.confirm(data.msg, funcSubmit);
                    }
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });

    }
});